/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.display.renderers {
	import flash.display.Graphics;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import stencil.core.engine.ISceneObject;
	import stencil.core.engine.MapNode;
	import stencil.core.engine.Position;
	
	import stencil.core.engine.display.ITileRenderer;
	import stencil.core.engine.display.SpriteObject;
	import stencil.core.engine.ITileSection;
	import stencil.core.engine.display.IDisplayObject;
	
	/**
	 * Strategy based tile render.
	 * 
	 * @author Max Zhuravkov
	 */
	public class TileRenderer extends TileRendererBase {
		
		private var _strategy:ITileRender;
		
		private var _tiles:Array = new Array();
		
		private var _tilesMap:Dictionary = new Dictionary();
		
		private var _sprites:Dictionary = new Dictionary();
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function TileRenderer(strategy:ITileRender) {
			super();
			
			this._strategy = strategy;
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		override protected function beforeRender():void {
			super.beforeRender();
			
			this._strategy.setup(this.grid, this.tileSettings);
			
			if (this.flagShowGridChanged) {
				for each(var section:ITileSection in this.sections) {
					var sprite:SpriteObject = this.getDisplayObject(section) as SpriteObject;
					var g:Graphics = sprite.graphics;
					
					g.clear();
					
					if (!this.showGrid) {
						continue;
					}
					
					this.drawGrid(g, section);
				}
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function createDisplayObject(section:ITileSection):IDisplayObject {
			var tiles:IDisplayObject = this._strategy.createTiles(section);
			var sprite:SpriteObject = new SpriteObject();
			
			tiles.owner = section;
			
			this._tilesMap[tiles.id] = tiles;
			
			sprite.id = tiles.id;
			sprite.flagBackground = section.flagBackground;
			sprite.flagTile = true;
			sprite.flagStatic = section.flagStatic;
			sprite.addChild(tiles.content);
			
			return sprite;
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function drawTileSection(section:ITileSection, destination:IDisplayObject):void {
			var tiles:IDisplayObject = this._tilesMap[destination.id];
			var sprite:SpriteObject = destination as SpriteObject;
			
			this._strategy.drawTiles(section, tiles);
			
			if (this.showGrid) {
				this.drawGrid(sprite.graphics, section);
				sprite.cacheAsBitmap = true;
			}
			
			sprite.owner = section;
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function updateTileSection(section:ITileSection, displayObject:IDisplayObject, redrawFlag:Boolean):void {
			var tiles:IDisplayObject = this._tilesMap[displayObject.id];
			
			this._strategy.updateTiles(section, tiles, redrawFlag);
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function releaseTiles(displayObject:IDisplayObject, section:ITileSection):void {
			var tiles:IDisplayObject = this._tilesMap[displayObject.id];
			var sprite:SpriteObject = displayObject as SpriteObject;
			
			sprite.removeChild(tiles.content);
			
			delete this._sprites[displayObject.id];
			
			this._strategy.releaseTiles(section, tiles);
		}
		
		protected function drawGrid(graphics:Graphics, section:ITileSection):void {
			var rect:Rectangle = section.rect;
			var sx:Number = section.rect.x;
			var sy:Number = section.rect.y;
			
			var p0:Point = grid.getCellPosition(rect.left - sx, rect.top - sy, false);
			var p1:Point = grid.getCellPosition(rect.right - sx, rect.top - sy, false);
			var p2:Point = grid.getCellPosition(rect.right - sx, rect.bottom - sy, false);
			var p3:Point = grid.getCellPosition(rect.left - sx, rect.bottom - sy, false);
			
			graphics.clear();
			
			if (this.lineStyle) {
				this.lineStyle.applyLineStyle(graphics);
			}
			
			if (this.fillStyle) {
				this.fillStyle.applyFillStyle(graphics);
			}
			
			graphics.moveTo(p0.x + p1.x, p0.y);
			graphics.lineTo(p1.x + p1.x, p1.y);
			graphics.lineTo(p2.x + p1.x, p2.y);
			graphics.lineTo(p3.x + p1.x, p3.y);
			graphics.lineTo(p0.x + p1.x, p0.y);
			
			var a:Point = new Point();
			var b:Point = new Point();
			
			for (var i:int = 0; i < rect.width; i ++) {
				this.grid.calculateCellPosition(i, 0, false, a); 
				this.grid.calculateCellPosition(i, rect.height, false, b);
				
				a.x += p1.x;
				b.x += p1.x;
				
				graphics.moveTo(a.x, a.y);
				graphics.lineTo(b.x, b.y);
			}
			
			for (var j:int = 0; j < rect.height; j ++) {
				this.grid.calculateCellPosition(0, j, false, a); 
				this.grid.calculateCellPosition(rect.width, j, false, b);
				
				a.x += p1.x;
				b.x += p1.x;
				
				graphics.moveTo(a.x, a.y);
				graphics.lineTo(b.x, b.y);
			}
			
			if (this.fillStyle) {
				graphics.endFill();
			}
			
		}
	}

}